package org.lee;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
import java.util.logging.SimpleFormatter;

/**
 * @author jackielee
 * @version 1.0
 * @description TaskService
 * @date 2021/7/2
 **/
public class TaskService {
    private ExecutorService executor = Executors.newFixedThreadPool(5);
    private ScheduledExecutorService executorService1 = Executors.newSingleThreadScheduledExecutor();
    private ScheduledExecutorService executorService2 = Executors.newScheduledThreadPool(3);

    public void start() throws InterruptedException, ExecutionException {
        System.out.println(System.currentTimeMillis());
        System.out.println(System.currentTimeMillis()/1000);

        executor.execute(()->{
            while (true){
                System.out.println(executor.isTerminated());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        executor.execute(()->{
            System.out.println("this is the time execution delay"+(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
        });
        for(int i = 0; i < 100;++i){
            FutureTask<String> task = new FutureTask<>(new Job());
            executor.submit(task);
            String val = task.get();
            System.out.println("the job result:"+val);
            Thread.sleep(10);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        executorService1.scheduleAtFixedRate(()-> System.out.println(sdf.format(new Date())+": by 1s later and per 5s call"),1,5, TimeUnit.SECONDS);
        executorService1.execute(()-> System.out.println("how the 1 time execute"));

        executorService2.scheduleWithFixedDelay(()->System.out.println(sdf.format(new Date())+": schedules by 1s later and per 5s call"), 1, 5, TimeUnit.SECONDS);

    }
}
